class Node {
  constructor(key, left = null, right = null) {
    this.key = key;
    this.left = left;
    this.right = right;
  }
}
function getNewNode(key) {
  return new Node(key);
}
function clear(root) {
  if (root == null) return;
  delete root.left;
  delete root.right;
}
function insert(root, key) {
  if (root == null) return getNewNode(key);
  if (root.key === key) return root;
  if (key < root.key) root.left = insert(root.left, key);
  else root.right = insert(root.right, key);
  return root;
}
function predeccessor() {
  let tmp = root.left;
  while (tmp.right) tmp = tmp.right;
  return tmp;
}
function erase(root, ley) {
  if (root == null) return root;
  if (key < root.key) {
    root.left = erase(root.left, key);
  } else if (key > root.key) {
    root.right = erase(root.right, key);
  } else {
    if (root.left == null && root.right === null) {
      return null;
    } else if (root.left == null || root.right == null) {
      let tmp = root.left || root.right;
      return tmp;
    } else {
      let tmp = preduccessor(root);
      root.key = temp.key;
      root.left = erase(root.left, tmp.key);
    }
  }
  return root;
}
let n = null;
let arr = [3, 4, 2];
arr.forEach((v) => {
  n = insert(n, v);
});
console.log(JSON.stringify(n, " ", 2));
